package com.jcl.core.web.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @ClassName CostTimeFilter

 * @Author yyliu
 * @Date 2018/9/6 9:33
 * @Version 1.0
 **/
//@WebFilter(urlPatterns = "/*", filterName = "costTimeFilter")
public class CostTimeFilter implements Filter {

    private static final Logger logger = LoggerFactory.getLogger(CostTimeFilter.class);

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request,
                         ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;

        long startTime = System.currentTimeMillis();
        logger.info("Request-URL: {}", req.getRequestURI());
        chain.doFilter(request, response);
        long endTime = System.currentTimeMillis() - startTime;
        logger.info("END-Time: {} ms, URL: {}", endTime, req.getRequestURI());

    }

    @Override
    public void destroy() {

    }
}
